package com.zsr.filter;

import com.zsr.utils.JdbcUtils;

import javax.servlet.*;
import java.io.IOException;

public class TransactionFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        try {
            System.out.println("TransactionFilter前置");

            filterChain.doFilter(servletRequest,servletResponse);
            System.out.println("TransactionFilter后置");
            JdbcUtils.commitAndClose();//提交事务
        } catch (Exception e) {
            JdbcUtils.rollbackAndClose();//回滚事务
            System.out.println("TransactionFilter异常");
            e.printStackTrace();
            throw new RuntimeException(e);//把异常抛给Tomcat
        }
    }
}
